//
//  MusicViewController.m
//  Relax
//
//  Created by yanghuiyan on 16/4/18.
//  Copyright © 2016年 yanghuiyan. All rights reserved.
//

#import "MusicViewController.h"
#import "MusicBigDataModel.h"
#import "MusicDetailDataModel.h"
#import "MusicChannelCollectionViewCell.h"
#import "MusicHotListCollectionViewCell.h"
#import "MusicNewSongCVC.h"
#import "MusicRecommendCVC.h"
#import "MusicSingerCVC.h"
#import "DCPicScrollView.h"
#import "MusicSongRecommendCVC.h"
#import "SongRemmondReusableView.h"
#import "NewSongReusableView.h"
#import "HotListReusableView.h"
#import "RemmondReusableView.h"
#import "SingerReusableView.h"
#import "SongListViewController.h"
@interface MusicViewController ()<UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>

@property (nonatomic, retain) YHYBaseCollectionView *collectionView;

@property (nonatomic, retain) NSMutableArray *dataSource;

@property (nonatomic, retain) NSMutableArray *imageArr;

@property (nonatomic, retain) NSMutableArray *detailArr;

@property (nonatomic, retain) NSMutableArray *arrChannel;/**< 频道 */

@property (nonatomic, retain) NSMutableArray *arrSongRemmond;/**< 歌曲推荐 */

@property (nonatomic, retain) NSMutableArray *arrNewSong;/**< 新歌新碟 */

@property (nonatomic, retain) NSMutableArray *arrHotlist;/**< 热门歌单 */

@property (nonatomic, retain) NSMutableArray *arrRemmond;/**< 推荐榜单 */

@property (nonatomic, retain) NSMutableArray *arrSinger;/**< 独家歌手 */

@property (nonatomic, retain) MusicBigDataModel *dataModel;

@property (nonatomic, retain) MusicDetailDataModel *detailDataModel;

@property (nonatomic, retain) YHYBaseView *topView;

@property (nonatomic, retain) DCPicScrollView *picSCrollView;

@property (nonatomic, retain) UICollectionViewFlowLayout *flowLayout;

@end

@implementation MusicViewController

- (void)dealloc {
    
    _collectionView.delegate = nil;
    _collectionView.dataSource = nil;
    
    [_dataSource release];
    [_imageArr release];
    [_detailArr release];
    [_dataModel release];
    [_arrSongRemmond release];
    [_arrSinger release];
    [_arrHotlist release];
    [_arrRemmond release];
    [_detailDataModel release];
    [_topView release];
    [_picSCrollView release];
    [_collectionView release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
//    self.automaticallyAdjustsScrollViewInsets = NO;

    
    self.dataSource = [NSMutableArray array];
    self.detailArr = [NSMutableArray array];
    self.imageArr = [NSMutableArray array];
    _arrChannel = [NSMutableArray array];
    _arrRemmond = [NSMutableArray array];
    _arrHotlist = [NSMutableArray array];
    _arrSinger = [NSMutableArray array];
    _arrSongRemmond = [NSMutableArray array];
    [self dataHandle];
    [self creatView];
    
}

#pragma mark - 数据解析
- (void) dataHandle {
    
    [YHYAFNetTool GETNetWirhUrl:@"http://api.dongting.com/frontpage/frontpage?f=f5004&s=s200&v=v9.0.0.2016041414&version=1461659862576" body:nil headerFile:nil response:YHYJSON Success:^(id result) {
        
        for (NSDictionary *dic in result[@"data"]) {
            self.dataModel = [[MusicBigDataModel alloc] initWithDataSource:dic];
            [self.dataSource addObject:_dataModel];
        }
        NSLog(@"%@", self.dataSource);
        
        [self.dataSource removeObject:self.dataSource.firstObject];
        [self.dataSource removeObject:self.dataSource.lastObject];
        [self.dataSource removeObject:self.dataSource.lastObject];
        NSLog(@"%ld", self.dataSource.count);
        
        for (NSDictionary *dic in result[@"data"][0][@"data"]) {
            MusicDetailDataModel *detailModel = [[MusicDetailDataModel alloc] initWithDataSource:dic];
            [self.imageArr addObject:detailModel.picUrl];
        }
        
        [self topViewA];
        
        [self.collectionView reloadData];
        
    } Failure:^(NSError *error) {
        
    }];
    
}


#pragma mark - 头部轮播
- (void)topViewA {
    
    self.topView = [[YHYBaseView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT * 0.25)];
    self.picSCrollView = [DCPicScrollView picScrollViewWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT * 0.25) WithImageUrls:self.imageArr];
    self.topView.backgroundColor = [UIColor grayColor];
    
    [self.topView addSubview:self.picSCrollView];
    [self.view addSubview:self.topView];
    [self.view bringSubviewToFront:self.topView];
    
    [_topView release];
    [_picSCrollView release];
    
}

#pragma mark - 创建视图
- (void)creatView {
    
    self.view.backgroundColor = [UIColor colorWithRed:247 / 255.0 green:248 / 255.0 blue:249 / 255.0 alpha:1];
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 200, 40)];
    titleLabel.text = @"音乐";
    titleLabel.font = [UIFont boldSystemFontOfSize:22];
    titleLabel.textColor = [UIColor grayColor];
    titleLabel.textAlignment = NSTextAlignmentCenter;
    self.navigationItem.titleView = titleLabel;
    [titleLabel release];
    
//    //导航栏右侧按钮
//    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"搜索"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal ] style:UIBarButtonItemStylePlain target:self action:@selector(rightItemAction:)];
//    self.navigationItem.rightBarButtonItem = rightItem;
//    [rightItem release];
    
    self.flowLayout = [[UICollectionViewFlowLayout alloc] init];
    _flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
    self.collectionView = [[YHYBaseCollectionView alloc] initWithFrame:CGRectMake(0, HEIGHT * 0.25 - 20, WIDTH, HEIGHT * 0.75 - 64) collectionViewLayout:_flowLayout];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    self.collectionView.bounces = NO;
    self.collectionView.backgroundColor = [UIColor colorWithRed:247 / 255.0 green:248 / 255.0 blue:249 / 255.0 alpha:1];
    // 频道入口
    [self.collectionView registerClass:[MusicChannelCollectionViewCell class] forCellWithReuseIdentifier:@"Channel"];
    // 歌曲推荐
    [self.collectionView registerClass:[MusicSongRecommendCVC class] forCellWithReuseIdentifier:@"SongRecommend"];
    // 新歌新碟 && 独家艺人
    [self.collectionView registerClass:[MusicNewSongCVC class] forCellWithReuseIdentifier:@"NewSong"];
    // 热门歌单 && 推荐榜单
    [self.collectionView registerClass:[MusicHotListCollectionViewCell class] forCellWithReuseIdentifier:@"HotList"];
    
    /**
     *  注册头视图重用池
     */
    // 歌曲推荐
    [self.collectionView registerClass:[SongRemmondReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"SongRemmond"];
    // 新歌新碟
    [self.collectionView registerClass:[NewSongReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"NewSong"];
    // 热门歌单
    [self.collectionView registerClass:[HotListReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"HotList"];
    // 推荐榜单
    [self.collectionView registerClass:[RemmondReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"Remmond"];
    // 独家艺人
    [self.collectionView registerClass:[SingerReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"Singer"];
    
    [self.view addSubview:self.collectionView];
    
}

- (void)rightItemAction:(UIBarButtonItem *)sender {
    
}

#pragma mark - UICollectionViewDelegateFlowLayout
// layout 布局 根据分区 返回不同的layout大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    switch (indexPath.section) {
            // 频道入口
        case 0: {
            _flowLayout.minimumLineSpacing = 0;
            _flowLayout.minimumInteritemSpacing = 0;
            _flowLayout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
            return CGSizeMake(WIDTH / 3, HEIGHT * 0.13);
            break;
        }
            // 歌曲推荐
        case 1: {
            _flowLayout.minimumLineSpacing = 0;
            _flowLayout.minimumInteritemSpacing = 0;
            _flowLayout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
            
            return CGSizeMake(WIDTH / 2, HEIGHT * 0.15);
            break;
        }
            // 新歌新碟
        case 2: {
            _flowLayout.minimumLineSpacing = 0;
            _flowLayout.minimumInteritemSpacing = 0;
            _flowLayout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);

            return CGSizeMake(WIDTH / 3, HEIGHT * 0.24);
            break;
        }
            // 热门歌单
        case 3: {
            _flowLayout.minimumLineSpacing = 0;
            _flowLayout.minimumInteritemSpacing = 0;
            _flowLayout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);

            return CGSizeMake(WIDTH / 3, HEIGHT * 0.24);
            break;
        }
            // 榜单推荐
        case 4: {
            _flowLayout.minimumLineSpacing = 0;
            _flowLayout.minimumInteritemSpacing = 0;
            _flowLayout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);

            return CGSizeMake(WIDTH / 3, HEIGHT * 0.24);
            break;
        }
            // 独家艺人
        default:{
            _flowLayout.minimumLineSpacing = 0;
            _flowLayout.minimumInteritemSpacing = 0;
            _flowLayout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);

            return CGSizeMake(WIDTH / 3, HEIGHT * 0.24);
            break;
        }
   }

    return CGSizeMake(0, 0);
}

#pragma mark - 返回头视图方法
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
    
    if (kind == UICollectionElementKindSectionHeader) {
        // 歌曲推荐
        if (indexPath.section == 1) {
            SongRemmondReusableView * SongRemmondHeader = [self.collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"SongRemmond" forIndexPath:indexPath];
            return SongRemmondHeader;
        }
        // 新歌新碟
        if (indexPath.section == 2) {
            NewSongReusableView * NewSongHeader = [self.collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"NewSong" forIndexPath:indexPath];
            return NewSongHeader;
        }
        // 热门歌单
        if (indexPath.section == 3) {
            HotListReusableView * HotListHeader = [self.collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"HotList" forIndexPath:indexPath];
            return HotListHeader;
        }
        // 推荐榜单
        if (indexPath.section == 4) {
            RemmondReusableView * RemmondHeader = [self.collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"Remmond" forIndexPath:indexPath];
            return RemmondHeader;
        }
        // 独家艺人
        if (indexPath.section == 5) {
        SingerReusableView * SingerHeader = [self.collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"Singer" forIndexPath:indexPath];
        return SingerHeader;
        }
    }
    
    return nil;
}

#pragma mark - ------------ 头视图高度 ---------------
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section {
    
    if(section == 0) {
        return CGSizeMake(0, 0);;
    }
    return CGSizeMake(WIDTH, 30);
}

#pragma mark - UICollectionViewDelegate

#pragma mark - ------------ 返回section ---------------
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return self.dataSource.count;
}

#pragma mark - ------------ 返回item ---------------
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
    switch (section) {
            // 频道入口
        case 0: {
            return 3;
            break;
        }
            // 歌曲推荐
        case 1: {
            return 2;
            break;
        }
            // 新歌新碟
        case 2: {
            return 3;
            break;
        }
            // 热门歌单
        case 3: {
            return 6;
            break;
        }
            // 推荐榜单
        case 4: {
            return 3;
            break;
        }
            // 独家艺人
        default:{
            return 3;
            break;
        }
    }
            return 0;

}

#pragma mark - ------------ 返回cell ---------------
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    switch (indexPath.section) {
            // 频道入口
        case 0: {
            MusicChannelCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Channel" forIndexPath:indexPath];
            cell.backgroundColor = [UIColor colorWithRed:0.14 green:0.18 blue:0.22 alpha:1.00];
            MusicBigDataModel *bigDataModel = self.dataSource[indexPath.section];
            MusicDetailDataModel *detailModel = bigDataModel.data[indexPath.item];
            cell.detailModel = detailModel;
            return cell;
            break;
        }
            // 歌曲推荐
        case 1: {
            MusicSongRecommendCVC *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"SongRecommend" forIndexPath:indexPath];
            cell.backgroundColor = [UIColor colorWithRed:0.16 green:0.20 blue:0.25 alpha:1.00];
            cell.layer.borderColor = [UIColor colorWithRed:0.26 green:0.20 blue:0.40 alpha:1.00].CGColor;
            cell.layer.borderWidth = 1;
            MusicBigDataModel *bigDataModel = self.dataSource[indexPath.section];
            MusicDetailDataModel *detailModel = bigDataModel.data[indexPath.item];
            cell.detailModel = detailModel;
            return cell;
            break;
        }
            // 新歌新碟
        case 2: {
            MusicNewSongCVC *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"NewSong" forIndexPath:indexPath];
            cell.backgroundColor = [UIColor colorWithRed:0.14 green:0.18 blue:0.22 alpha:1.00];
            MusicBigDataModel *bigDataModel = self.dataSource[indexPath.section];
            MusicDetailDataModel *detailModel = bigDataModel.data[indexPath.item];
            cell.detailModel = detailModel;
            return cell;
            break;
        }
            // 热门歌单
        case 3: {
            MusicHotListCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"HotList" forIndexPath:indexPath];
            cell.backgroundColor = [UIColor colorWithRed:0.16 green:0.20 blue:0.25 alpha:1.00];
            MusicBigDataModel *bigDataModel = self.dataSource[indexPath.section];
            MusicDetailDataModel *detailModel = bigDataModel.data[indexPath.item];
            cell.detailModel = detailModel;
            return cell;
            break;
        }
            // 推荐榜单
        case 4: {
            MusicHotListCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"HotList" forIndexPath:indexPath];
            MusicBigDataModel *bigDataModel = self.dataSource[indexPath.section];
            MusicDetailDataModel *detailModel = bigDataModel.data[indexPath.item];
            cell.detailModel = detailModel;
            cell.backgroundColor = [UIColor colorWithRed:0.14 green:0.18 blue:0.22 alpha:1.00];
            cell.remmondModel = detailModel;
            return cell;
            break;
        }
            // 独家艺人
        default:{
            MusicNewSongCVC *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"NewSong" forIndexPath:indexPath];
            cell.backgroundColor = [UIColor colorWithRed:0.16 green:0.20 blue:0.25 alpha:1.00];
            MusicBigDataModel *bigDataModel = self.dataSource[indexPath.section];
            MusicDetailDataModel *detailModel = bigDataModel.data[indexPath.item];
            cell.newModel = detailModel;
            return cell;
            break;
        }
    }
    return nil;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.section == 0) {
        if (indexPath.item == 0) {
            
            SongListViewController *songListVC = [[SongListViewController alloc] init];
            songListVC.type = @"全部>";
            [self.navigationController pushViewController:songListVC animated:YES];
            
        }
    }
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
